home *** CD-ROM | disk | FTP | other *** search
/ Aminet 31 / Aminet 31 (1999)(Schatztruhe)[!][Jun 1999].iso / Aminet / util / libs / chunky_dev.lha / chunky_dev / Demos / Colours / Colours.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-17  |  5.4 KB  |  208 lines

  1. //
  2. // chunky.library demo
  3. //
  4. // This example demonstrates all the colour functions provided by
  5. // chunky.library version 4+.
  6. //
  7. // (c) 1999 Rosande Limited, all rights reserved.
  8. // PUBLIC DOMAIN
  9. //
  10. // http://www.irrelevant.org/~oondy/chunky/
  11.  
  12. #include <exec/types.h>
  13. #include <exec/libraries.h>
  14. #include <clib/chunky_protos.h>
  15. #include <libraries/chunky.h>
  16. #include <pragma/chunky_lib.h>
  17. #include <intuition/intuition.h>
  18. #include <pragma/intuition_lib.h>
  19. #include <pragma/graphics_lib.h>
  20. #include <pragma/exec_lib.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24.  
  25. #include "/_shared/screen.h"
  26. #include "/_shared/waitbutton.h"
  27. #include "/_shared/requester.h"
  28. #include "bartcolours.h"
  29.  
  30. struct Library *ChunkyBase;
  31. struct ChunkyPort *cp = NULL;
  32. struct ColoursCP *Cols = NULL;
  33.  
  34. void DoColourDemo(void);
  35. void DisplayColours(void);
  36. void DemoSetRGB4(void);
  37. void DemoGetRGB4(void);
  38.  
  39. main()
  40. {
  41.   if(ChunkyBase = OpenLibrary("chunky.library", 4))
  42.   {
  43.     if(DEMO_OpenScreen(640, 480, NULL, NULL))
  44.     {
  45.       if(cp = CHK_InitChunky(32, 32))
  46.       {
  47.         CHK_ChooseHardwareMode(GetVPModeID(&DemoScreen->ViewPort));
  48.         DoColourDemo();
  49.       }
  50.     }
  51.   }
  52.   if(cp) CHK_FreeChunky(cp);
  53.   if(DemoScreen) DEMO_CloseScreen();
  54.   if(ChunkyBase) CloseLibrary(ChunkyBase);
  55. }
  56.  
  57. void DoColourDemo(void)
  58. {
  59.   DEMO_Request("Oh my, hello!\nThis is an example of using all the\n"
  60.                "colour table functions supplied by chunky.library.\n\n"
  61.                "CHK_InitColours(), CHK_PutChunkyColours(),\n"
  62.                "CHK_ConvertCMAP(), CHK_SetRGB32(), CHK_SetRGB4(),\n"
  63.                "CHK_GetRGB32(), CHK_GetRGB4() and CHK_LoadRGB32().",
  64.                NULL, NULL);
  65.  
  66.   // Give our chunkyport  a colour table
  67.   if(Cols = CHK_InitColours(cp))
  68.   {
  69.     // Insert our LoadRGB32() colour table into the cp's table
  70.     CHK_LoadRGB32(cp, (void *)&BartRGB32);
  71.  
  72.     DEMO_Request("We have colours initialised in the colour table.\n"
  73.                  "Let me display them all to you...", "Neat", NULL);
  74.  
  75.     DisplayColours();
  76.  
  77.     DEMO_Request("Woah!  Look at all the colours, man!\n\n"
  78.                  "So far, we've demonstated CHK_InitColours(), CHK_LoadRGB32(),\n"
  79.                  "and CHK_PutChunkyColours().  Now, let's set all the colours\n"
  80.                  "from colour 4 to 255 as white using CHK_SetRGB4().",
  81.                  "What a boring colour", NULL);
  82.  
  83.     DemoSetRGB4();
  84.     DemoGetRGB4();
  85.   }
  86.   else
  87.   {
  88.     printf("No mem for colourtable\n");
  89.   }
  90. }
  91.  
  92. #define YOFF 20
  93. #define XOFF 12
  94. #define BOXWIDTH  16
  95. #define BOXHEIGHT 16
  96. void DisplayColours(void)
  97. {
  98.   int i, x, y, x2, y2;
  99.   struct RastPort *rp = DemoWindow->RPort;
  100.   char t[8];
  101.  
  102.   CHK_PutChunkyColours(cp, &DemoScreen->ViewPort);
  103.   SetRast(DemoWindow->RPort, 0);
  104.  
  105.   y = YOFF + 16; x = XOFF;
  106.   for(i = 0; i < 256; i++)
  107.   {
  108.     SetAPen(rp, 1); sprintf(t, "%ld", i);
  109.     Move(rp, x, y+8); Text(rp, t, strlen(t));
  110.     x += (3*8)+2; x2 = (x+BOXWIDTH)-1; y2 = (y+BOXHEIGHT)-1;
  111.     SetAPen(rp, i); RectFill(rp, x, y, x2, y2);
  112.     SetAPen(rp, 1);
  113.     Move(rp, x, y);
  114.     Draw(rp, x2, y); Draw(rp, x2, y2); Draw(rp, x, y2); Draw(rp, x, y);
  115.     x += BOXWIDTH+2;
  116.     if(x > (DemoWindow->Width - (BOXWIDTH + (8*3)+2)))
  117.     {
  118.       x = XOFF; y += YOFF+2;
  119.     }
  120.   }
  121. }
  122.  
  123. int randmax(int n) { int i; for(;;) { i = rand(); if(!(i>n)) return(i); } }
  124.  
  125. void DemoSetRGB4(void)
  126. {
  127.   int i;
  128.  
  129.   for(i = 4; i < 256; i++)
  130.     CHK_SetRGB4(cp, i, 255, 255, 255);
  131.  
  132.   DEMO_Request("Done.. hit them with the lights!", NULL, NULL);
  133.   DisplayColours();
  134.  
  135.   DEMO_Request("Now for some random 0-255 colours...", NULL, NULL);
  136.   for(i = 4; i < 256; i++)
  137.     CHK_SetRGB4(cp, i, randmax(255), randmax(255), randmax(255));
  138.   DisplayColours();
  139.  
  140.   DEMO_Request("CHK_SetRGB32() works just like CHK_SetRGB4() except that\n"
  141.                "it takes 32-bit left-justifed values like\n"
  142.                "graphics.library/SetRGB32().", "I see", NULL);
  143. }
  144.  
  145. void DemoGetRGB4(void)
  146. {
  147.   int i, x, y;
  148.   char s[80], label1[] = "Click the close gadget to use CHK_GetRGB32()!\0",
  149.               label2[] = "Click the close gadget to quit!\0",
  150.               label3[] = "Click the close gadget for more...\0";
  151.   struct RastPort *rp = DemoWindow->RPort;
  152.   struct cp_Colour4 col;
  153.   struct cp_Colour32 *col32;
  154.  
  155.   DEMO_Request("Now then... what are the RGB values of all these colours?\n"
  156.                "Let's use CHK_GetRGB4() to find out...", "Wicked", NULL);
  157.  
  158.   SetRast(DemoWindow->RPort, 0);
  159.  
  160.   x = 4; y = 12;
  161.   for(i = 0; i < 256; i++)
  162.   {
  163.     CHK_GetRGB4(cp, i, &col);
  164.     Move(rp, x, y);
  165.     sprintf(s, "Col %3d - R: %3d G: %3d B: %3d  ", i, col.R, col.G, col.B);
  166.     Text(rp, s, strlen(s));
  167.     x += TextLength(rp, s, strlen(s)); if(x > 400) { x = 4; y += 10; }
  168.  
  169.     if(y > 460)
  170.     {
  171.       Move(rp, 4, 465);
  172.       Text(rp, label3, strlen(label3));
  173.       DEMO_WaitForCloseGadget();
  174.       x = 4; y = 12;
  175.       SetRast(DemoWindow->RPort, 0);
  176.     }
  177.   }
  178.  
  179.   Move(rp, 4, y + 12); Text(rp, label1, strlen(label1));
  180.   DEMO_WaitForCloseGadget();
  181.  
  182.   // GetRGB32() demo
  183.  
  184.   SetRast(DemoWindow->RPort, 0);
  185.  
  186.   x = 4; y = 12;
  187.   for(i = 0; i < 256; i++)
  188.   {
  189.     col32 = CHK_GetRGB32(cp, i);
  190.     Move(rp, x, y);
  191.     sprintf(s, "Col %3d - R: 0x%08X G: 0x%08X B: 0x%08X  ", i, col32->R, col32->G, col32->B);
  192.     Text(rp, s, strlen(s));
  193.     x += TextLength(rp, s, strlen(s)); if(x > 400) { x = 4; y += 10; }
  194.  
  195.     if(y > 460)
  196.     {
  197.       Move(rp, 4, 465);
  198.       Text(rp, label3, strlen(label3));
  199.       DEMO_WaitForCloseGadget();
  200.       x = 4; y = 12;
  201.       SetRast(DemoWindow->RPort, 0);
  202.     }
  203.   }
  204.  
  205.   Move(rp, 4, y + 12); Text(rp, label2, strlen(label2));
  206.   DEMO_WaitForCloseGadget();
  207. }
  208.